package news.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.ViewUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import base.BaseFragment;
import shanshuitiancheng.news.R;

/**
 * Created by cch on 2016/6/17.
 */
public class MenuFragment2 extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView lv1;
    private ListView lv2;
    private ListView lv3;
    private MenuAdapter adapter;


    @Override
    public View initView(LayoutInflater inflater) {
          View view = inflater.inflate(R.layout.layout_left_menu,null);
       TextView tv = (TextView) view.findViewById(R.id.tv_menu_classify);
        lv1 = (ListView) view.findViewById(R.id.lv_menu_news_center);
        lv2 = (ListView) view.findViewById(R.id.lv_menu_smart_service);
        lv3 = (ListView) view.findViewById(R.id.lv_menu_govaffairs);

        lv1.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }
    private List<String> menulist = new ArrayList<String>();
    private View view;
    public void initMenu(List<String> menuNewCenterList) {
        menulist.clear();//清理下里面的数据，确保里面没有任何杂质，以确定传过来的数据是准确的.
        menulist.addAll(menuNewCenterList);
        //判断adapter中是否有我们要添加的信息，如果是空的直接添加，如果有其他的信息的话
        //用notifyDataSetChanged方法来通知如果适配器的内容改变时需要强制调用getView来刷新每个Item的内容。
        //notifyDataSetChanged()，重绘当前可见区域
        if (adapter == null){
            adapter = new MenuAdapter(ct,menulist);
            lv1.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
        adapter.setCurPostion(0);//初始化当你进去滑动

       /* adapter = new MenuAdapter(ct,menulist);//没有判断之前的代码
         lv1.setAdapter(adapter);*/
    }

    @Override
    //设置点击事件，当点击时会相应事件！
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            adapter.setCurPostion(position);
    }

    private class MenuAdapter extends BaseAdapter{
        private Context ct;
        private List<String> menuList;
        //当前选中的位置
        private int curPostion = 0;
        public MenuAdapter(Context ct, List<String> menulist) {
            this.ct = ct;
            this.menuList = menulist;
        }
        public  void setCurPostion(int postion){
                this.curPostion = postion;
            //notifyDataSetChanged()，重绘当前可见区域
                notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return menuList.size();
        }

        @Override
        public Object getItem(int position) {
            return menuList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = view.inflate(ct,R.layout.layout_item_menu,null);
            }
           TextView tv = (TextView) convertView.findViewById(R.id.tv_menu_item);
            ImageView image = (ImageView) convertView.findViewById(R.id.iv_menu_item);
            tv.setText(menuList.get(position));
            if (curPostion == position){
                tv.setTextColor(getResources().getColor(R.color.red));
                image.setBackgroundResource(R.mipmap.menu_arr_select);
            }else {
                tv.setTextColor(getResources().getColor(R.color.white));
                image.setBackgroundResource(R.mipmap.menu_arr_normal);
            }
            return convertView;
        }
    }
}
